using System;
using System.Collections;
using System.Web.UI.WebControls;

namespace Sedna.Core.Controller.Configurations
{
	/// <summary>
	/// Summary description for DataGridExecutionContextCommandArgumentGenerator.
	/// </summary>
	public class DataGridExecutionContextCommandArgumentGenerator : IDataGridCommandArgumentsGenerator
	{
		public string GenerateCommandArguments(IDataGridColumnConfiguration columnConfiguration, DataGridItem dataGridItem, IExecutionContext executionContext)
		{
			string result = BuildArguments(executionContext);
			if(next != null)
                result += next.GenerateCommandArguments(columnConfiguration, dataGridItem, executionContext);
			return result;
		}

		private IDictionary contextNames; 
		private IDataGridCommandArgumentsGenerator next;

		public IDataGridCommandArgumentsGenerator Next
		{
			set { next = value; }
		}

		public IDictionary ContextNames
		{
			set { contextNames = value; }
		}

		private string BuildArguments(IExecutionContext executionContext)
		{
			if(contextNames == null || contextNames.Count == 0)
			{
				return string.Empty;
			}

			string result = "";
			for(IDictionaryEnumerator en = contextNames.GetEnumerator(); en.MoveNext();)
			{
				result += String.Format("{0}={1};", en.Key, executionContext[en.Value]);
			}
			return result;
		}
	}
}
